//
//  RCTagsController.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-24.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCTagsController.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"


#define ButtonBaseTag 200
@interface RCTagsController (){
    NSInteger _selected;
}

@end

@implementation RCTagsController

@synthesize controllers = _controllers;

- (void) dealloc
{
    [_controllers release];
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (id) initWithViewControllers:(NSArray *)controllers
{
    self = [super init];
    if (self) {
        _controllers = [controllers retain];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.titleLabelText = @"快速注册-如意行";
    self.navigationItem.titleView = [self createTitleView];

    _selected = 0;


    for (int i = 0; i < 2; i++) {
        UIButton *t1  = [[UIButton alloc] initWithFrame:CGRectMake(50+110*i, 15, 110, 25)];
        [self.view addSubview:t1];
        [t1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [t1 release];
        [t1 setTitleColor:[UIColor colorWithHex:0xff505050] forState:UIControlStateNormal];
        t1.titleLabel.font = [UIFont fontWithName:kFontNameHeiti size:16];
        if (i == 0) {
            [t1 setTitle:@"手机号注册" forState:UIControlStateNormal];
        }else{
            [t1 setTitle:@"邮箱注册" forState:UIControlStateNormal];
        }
        t1.tag = ButtonBaseTag + i;
    }
   
    [self setBtnSelectedState:0];
    [self setBtnUnSelectedState:1];
    [self displayContentController:[_controllers objectAtIndex:0]];

    UIView *horizonLine = [[UIView alloc]initWithFrame:CGRectMake(0, 49, 320, 1)];
    horizonLine.backgroundColor = [UIColor colorWithHex:0xffe6e6e6];
    [self.view addSubview:horizonLine];
    [horizonLine release];
    
}

- (void) btnClick:(id) sender
{
    NSInteger tag = [(UIButton *)sender tag];
    NSInteger lastSelect = _selected;
    
    if (_selected == -1) {
        return;
    }else{
        _selected = tag-ButtonBaseTag;
    }
    
    if (_selected != lastSelect) {
        [self setBtnSelectedState:_selected];
        [self setBtnUnSelectedState:lastSelect];
        [self hideContentController:[_controllers objectAtIndex:lastSelect]];
        [self displayContentController:[_controllers objectAtIndex:_selected]];
    }
  

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (CGRect) frameForContentController
{
    return CGRectMake(0, 50, 320, UIScreenHeight-50-44-20);
}

- (void) displayContentController: (UIViewController*) content;
{
    [self addChildViewController:content];                 // 1
    content.view.frame = [self frameForContentController]; // 2
    [self.view addSubview:content.view];
    [content didMoveToParentViewController:self];          // 3
}


- (void) hideContentController: (UIViewController*) content
{
    [content willMoveToParentViewController:nil];  // 1
    [content.view removeFromSuperview];            // 2
    [content removeFromParentViewController];      // 3
}

- (void) setBtnSelectedState:(NSInteger) index
{

    NSInteger tag = index + ButtonBaseTag;
    UIButton *btn = (UIButton *)[self.view viewWithTag:tag];

    UIColor *bgColor = [UIColor colorWithRed:255.0/255 green:107.0/255 blue:39.0/255 alpha:1];

    [btn setBackgroundImage:[UIColor createImageWithColor:bgColor] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIColor createImageWithColor:bgColor] forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

- (void) setBtnUnSelectedState:(NSInteger) index
{
    NSInteger tag = index + ButtonBaseTag;
    UIButton *btn = (UIButton *)[self.view viewWithTag:tag];
    [btn setBackgroundImage:nil forState:UIControlStateNormal];
    [btn setBackgroundImage:nil forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor colorWithHex:0xff505050] forState:UIControlStateNormal];

}
@end
